'use strict';

const Controller = require('egg').Controller;

class SystemDefineController extends Controller {

  async get() {
    const { ctx } = this;
    const { key } = ctx.query;
    const model = await ctx.model.SystemDefine.findOne({
      where: {
        key,
      },
    });
    ctx.body = model;
  }

  async add() {
    const { ctx } = this;
    const { key, value } = ctx.request.body;
    let model = await ctx.model.SystemDefine.findOne({
      where: {
        key,
      },
    });
    if (!model) {
      model = await ctx.model.SystemDefine.create({
        key,
        value,
      });
    } else {
      model.value = value;
      await model.save();
    }
    ctx.status = 201;
    ctx.body = model;
  }

}

module.exports = SystemDefineController;
